// pages/app_dao/app_dao.js
// 地图选点插件接口调用：
const chooseLocation = requirePlugin('chooseLocation');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mapCtx:'',
    qidian:{title:'当前位置',longitude:0,latitude:0},
    zhongdian:{title:'',longitude:0,latitude:0},

    // POI检索弹框
    add_tr:0,
    add_tr1:0,
    poi_num:[{title:"美食",num:1},{title:"公司企业",num:1},{title:"机构团体",num:1},{title:"购物",num:1},{title:"生活服务",num:1},{title:"娱乐休闲",num:1},{title:"运动健身",num:1},{title:"汽车",num:1},{title:"医疗保险",num:1},{title:"酒店宾馆",num:1},{title:"旅游景点",num:1},{title:"文化场馆",num:1},{title:"教育学校",num:1},{title:"银行金融",num:1},{title:"地名地址",num:1},{title:"基础设施",num:1},{title:"房产小区",num:1},{title:"室内及附属设施",num:1},{title:"其它",num:1}],
    sya:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  // 地图选点
  xuandian(){
    this.setData({add_tr:1});
    setTimeout(() => {
      this.setData({add_tr1:1});
    }, 30);
  },
  // 取消选择
  qu_remo(){
    this.setData({add_tr1:0});
    setTimeout(() => {
      this.setData({add_tr:0});
    }, 360);
  },
  // 地图开始选点
  one_go(){
    var tongyuan=[];
    for(let i of this.data.sya){
      tongyuan.push(this.data.poi_num[i].title)
    }
    const key = 'PQFBZ-DVMKJ-UZPFM-FNPY2-MV5ZZ-LDB3S'; //使用在腾讯位置服务申请的key
    const referer = '豆子行程'; //调用插件的app的名称
    // 传输固定的经纬度
    // const location = JSON.stringify({
    //   latitude: 39.89631551,
    //   longitude: 116.323459711
    // });
    const category = tongyuan.toString();
    wx.navigateTo({
      url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&category=${category}`
      // url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&category=${category}&location=${location}`
    });
    this.qu_remo();
  },
  // POI选择，最多选择三个
  poi_click(e){
    var index=e.currentTarget.dataset.index;
    console.log(index)

    if(this.data.poi_num[index].num==1){
      if(this.data.sya.length>=3){
        this.ermiao("最多选择三个关键词");
      }else{
        this.data.poi_num[index].num=2;
        this.data.sya.push(index)
        this.data.sya=[...new Set(this.data.sya)]
        this.setData({sya:this.data.sya,})
      }
      console.log(1)
      this.setData({poi_num:this.data.poi_num})
    }else if(this.data.poi_num[index].num==2){
      this.data.poi_num[index].num=1;
      this.data.sya.splice(this.data.sya.indexOf(index),1);
      console.log(2)
      this.setData({poi_num:this.data.poi_num,sya:this.data.sya})
    }
    console.log(this.data.poi_num[index],this.data.sya)
  },
  // 点击导航
  daoqu(){
    if(!this.data.zhongdian.title){
      this.ermiao("请选择终点位置");
    }else{
      this.data.mapCtx.openMapApp({
        longitude:this.data.zhongdian.longitude,
        latitude:this.data.zhongdian.latitude	,
        destination:this.data.zhongdian.title,
        success:res=>{
          console.log(res,"成功")
        },
        fail:res=>{
          console.log(res,"错误")
        }
      })
    }
  },
  // 弹框2s
  ermiao(val){
    wx.showToast({
      title: val,
      icon:'none',
      duration:2000
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 使用 wx.createMapContext 获取 map 上下文
    this.data.mapCtx = wx.createMapContext('myMap')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取地图选点返回的选择的结果
    const location_num = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
    console.log(location_num)
    if(location_num){
      this.setData({zhongdian:{title:location_num.name,longitude:location_num.longitude,latitude:location_num.latitude}})
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})